package com.cxso.example;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;
import com.cxso.example.client.MergeAgentWlptClient;
import com.cxso.example.client.MergeOwnerWlptClient;
import com.cxso.example.domain.wlpt.goods.ApiGoodsBasicDomain;
import com.cxso.example.domain.wlpt.goods.ApiGoodsDomain;
import com.cxso.example.domain.wlpt.goods.ApiGoodsLineDomain;
import com.cxso.example.domain.wlpt.owner.ApiOwnerAuthDomain;
import com.cxso.example.enums.wlpt.IsInvoiceEnum;
import com.cxso.example.request.wlpt.ApiConsignorGoodsCreateReq;
import com.cxso.example.request.wlpt.ApiConsignorOwnerCreateReq;
import com.cxso.example.response.wlpt.ApiConsignorGoodsCreateRes;
import com.cxso.example.response.wlpt.ApiConsignorOwnerCreateRes;
import com.cxso.example.sdk.SdkClient;
import com.cxso.example.sdk.domain.Result;
import com.cxso.example.sdk.http.client.ClientManager;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.HashMap;

/**
 * 聚合业务示例
 */
public class MergeExample {

    public static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //步骤一: 初始化客户端链接
    public static final ClientManager CLIENT_MANAGER = SdkClient.builder("服务域名地址", "clientId", "clientSecret", "rsaPrivateKey");

    public static void main(String[] args) {

        //<editor-fold desc="聚合代理商服务">
        //步骤二: 选择服务
        MergeAgentWlptClient mergeAgentWlptClient = CLIENT_MANAGER.use(MergeAgentWlptClient.class);
        //代理商接口: 创建货主
        //步骤三: 调用服务 getHeader 中传入合作编号ID
        Result<ApiConsignorOwnerCreateRes> result = mergeAgentWlptClient.ownerCreate(getTestOwnerData(), getHeader("xxx"));
        //步骤四: 拿到结果
        if (result.isSuccess()) {
            ApiConsignorOwnerCreateRes data = result.getData();
            System.out.println(JSONObject.toJSONString(data, JSONWriter.Feature.PrettyFormat));
        } else {
            System.out.println(result.getMsg());
        }
        //</editor-fold>


        //<editor-fold desc="聚合货主服务">
        //步骤二: 选择服务
        MergeOwnerWlptClient mergeOwnerWlptClient = CLIENT_MANAGER.use(MergeOwnerWlptClient.class);
        //代理商接口: 创建货主
        //步骤三: 调用服务 getHeader 中传入合作编号ID , 货主编号ID
        Result<ApiConsignorGoodsCreateRes> result2 = mergeOwnerWlptClient.goodsCreate(getTestGoods(), getHeader("xxx", "xxx"));
        //步骤四: 拿到结果
        if (result2.isSuccess()) {
            ApiConsignorGoodsCreateRes data = result2.getData();
            System.out.println(JSONObject.toJSONString(data, JSONWriter.Feature.PrettyFormat));
        } else {
            System.out.println(result2.getMsg());
        }
        //</editor-fold>
    }

    public static HashMap<String, String> getHeader(String cid) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("cid", cid);
        return hashMap;
    }

    public static HashMap<String, String> getHeader(String cid, String corpId) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("cid", cid);
        hashMap.put("corpId", corpId);
        return hashMap;
    }

    public static ApiConsignorOwnerCreateReq getTestOwnerData() {
        String phone = "13327437777";
        ApiConsignorOwnerCreateReq apiConsignorOwnerCreateReq = new ApiConsignorOwnerCreateReq();
        apiConsignorOwnerCreateReq.setOwnerPhone(phone);
        ApiOwnerAuthDomain apiOwnerAuthDomain = new ApiOwnerAuthDomain();
        apiOwnerAuthDomain.setBusinessLicense("测试营业执照");
        apiOwnerAuthDomain.setEnterpriseName(String.format("测试企业%s", phone));
        apiOwnerAuthDomain.setCreditCode("3010283718192719212");
        apiOwnerAuthDomain.setEnterpriseType("测试企业类型");
        apiOwnerAuthDomain.setEnterpriseAddr("测试注册地址");
        apiOwnerAuthDomain.setRegisteredCapital("293817");
        apiOwnerAuthDomain.setPeriodValidity("2035-01-01");
        apiOwnerAuthDomain.setBusinessScope("测试经营范围");
        apiOwnerAuthDomain.setCorporateIdCardFront("测试法人身份证正面");
        apiOwnerAuthDomain.setCorporateIdCardBack("测试法人身份证反面");
        apiOwnerAuthDomain.setCorporateName("测试法人姓名");
        apiOwnerAuthDomain.setCorporateIdCardNumber("29373918172729178172");
        apiOwnerAuthDomain.setCorporateIdCardValidityBegin("2025-01-01");
        apiOwnerAuthDomain.setCorporateIdCardValidityEnd("2035-01-01");
        apiOwnerAuthDomain.setContactName("测试联系人姓名");
        apiOwnerAuthDomain.setContactMobile(phone);
        apiOwnerAuthDomain.setContactEmail(String.format("%s@qq.com", phone));
        apiConsignorOwnerCreateReq.setOwnerAuthDomain(apiOwnerAuthDomain);
        return apiConsignorOwnerCreateReq;
    }

    public static ApiConsignorGoodsCreateReq getTestGoods() {
        ApiGoodsBasicDomain apiGoodsBasicDomain = new ApiGoodsBasicDomain();
        apiGoodsBasicDomain.setBodyId("674718f9d8e175f8f9eb74d35bdbe604");
        apiGoodsBasicDomain.setIsInvoice(IsInvoiceEnum.SPECIAL_INVOICE);
        apiGoodsBasicDomain.setGoodsCode(String.valueOf(System.currentTimeMillis()));

        ApiGoodsLineDomain apiGoodsLineDomain = new ApiGoodsLineDomain();
        apiGoodsLineDomain.setSendProvince("湖北省");
        apiGoodsLineDomain.setSendCity("武汉市");
        apiGoodsLineDomain.setSendArea("蔡甸区");
        apiGoodsLineDomain.setSendAddress("湖北省武汉市蔡甸区军山街道G4京港澳高速");
        apiGoodsLineDomain.setSendAddressRemark(null);
        apiGoodsLineDomain.setSendLon(new BigDecimal("114.117583"));
        apiGoodsLineDomain.setSendLat(new BigDecimal("30.386318"));
        apiGoodsLineDomain.setSendPerson("张三");
        apiGoodsLineDomain.setSendPhone("13476646726");
        apiGoodsLineDomain.setDeliveryProvince("湖北省");
        apiGoodsLineDomain.setDeliveryCity("武汉市");
        apiGoodsLineDomain.setDeliveryArea("江夏区");
        apiGoodsLineDomain.setDeliveryAddress("湖北省武汉市江夏区江夏区经济开发区梁子湖风景区办事后山村梁子湖风景区");
        apiGoodsLineDomain.setDeliveryAddressRemark(null);
        apiGoodsLineDomain.setDeliveryLon(new BigDecimal("114.553027"));
        apiGoodsLineDomain.setDeliveryLat(new BigDecimal("30.262731"));
        apiGoodsLineDomain.setDeliveryPerson("李四");
        apiGoodsLineDomain.setDeliveryPhone("13476646727");

        ApiGoodsDomain apiGoodsDomain = new ApiGoodsDomain();
        apiGoodsDomain.setGoodsType("大宗货物");
        apiGoodsDomain.setGoodsPrice(new BigDecimal("10000"));
        apiGoodsDomain.setGoodsName("石子");
        apiGoodsDomain.setGoodsWeight(new BigDecimal(12));


        ApiConsignorGoodsCreateReq apiConsignorGoodsCreateReq = new ApiConsignorGoodsCreateReq();
        apiConsignorGoodsCreateReq.setBasic(apiGoodsBasicDomain);
        apiConsignorGoodsCreateReq.setLine(apiGoodsLineDomain);
        apiConsignorGoodsCreateReq.setGoods(apiGoodsDomain);
        return apiConsignorGoodsCreateReq;
    }
}
